package com.yanqu.road.server.manager.group;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manger.SystemCrossServerGroupMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SystemGroupChatMgr extends TempMgr {

    public static final int MAX_MESSAGE_COUNT = 200;

    //systemId groupId
    private static Map<Integer, Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder>> chatMsgMap;

    //systemId,groupId
    private static Map<Integer, Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder>> serverSystemMsgMap;

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        chatMsgMap = new ConcurrentHashMap<>();
        serverSystemMsgMap = new ConcurrentHashMap<>();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void groupChat(int systemId, int groupId, WorldMessageProto.WorldMessageTempMsg chatMsg){
        Set<Long> serverIds = SystemCrossServerGroupMgr.getGroupServer(systemId, groupId);
        if(!chatMsgMap.containsKey(systemId)){
            synchronized (chatMsgMap){
                if(!chatMsgMap.containsKey(systemId)){
                    chatMsgMap.put(systemId, new ConcurrentHashMap<>());
                }
            }
        }
        Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder> tempChatMsgMap = chatMsgMap.get(systemId);
        if(!tempChatMsgMap.containsKey(groupId)){
            synchronized (tempChatMsgMap){
                if(!tempChatMsgMap.containsKey(groupId)){
                    tempChatMsgMap.put(groupId, WorldMessageProto.WorldMessageListMsg.newBuilder());
                }
            }
        }
        WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = tempChatMsgMap.get(groupId);
        synchronized (chatListMsg){
            if(chatListMsg.getDataListCount() >= MAX_MESSAGE_COUNT){
                chatListMsg.removeDataList(0);
            }
            chatListMsg.addDataList(chatMsg);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.A_WORLD_MESSAGE_TRANSFER, chatMsg);
        for(long serverId : serverIds){
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }

    public static void groupSystemChat(long serverId, int systemId, int groupId, WorldMessageProto.WorldMessageTempMsg chatMsg){
        Set<Long> serverIds = SystemCrossServerGroupMgr.getGroupServer(systemId, groupId);
        if(!serverSystemMsgMap.containsKey(systemId)){
            synchronized (serverSystemMsgMap){
                if(!serverSystemMsgMap.containsKey(systemId)){
                    serverSystemMsgMap.put(systemId, new ConcurrentHashMap<>());
                }
            }
        }
        Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder> tempChatMsgMap = serverSystemMsgMap.get(systemId);
        if(!tempChatMsgMap.containsKey(groupId)){
            synchronized (tempChatMsgMap){
                if(!tempChatMsgMap.containsKey(groupId)){
                    tempChatMsgMap.put(groupId, WorldMessageProto.WorldMessageListMsg.newBuilder());
                }
            }
        }
        WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = tempChatMsgMap.get(groupId);
        synchronized (chatListMsg){
            if(chatListMsg.getDataListCount() >= 50){
                chatListMsg.removeDataList(0);
            }
            chatListMsg.addDataList(chatMsg);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.A_WORLD_MESSAGE_TRANSFER, chatMsg);
        for(long sid : serverIds){
            MessageHelper.sendPacket(sid, 0, pbMsg);
        }
    }

    public static WorldMessageProto.WorldMessageListMsg.Builder getChatListMsg(int systemId, int groupId){
        if(!chatMsgMap.containsKey(systemId)){
            return null;
        }
        return chatMsgMap.get(systemId).get(groupId);
    }

    public static WorldMessageProto.WorldMessageListMsg.Builder getSystemChatListMsg(int systemId, int groupId){
        if(!serverSystemMsgMap.containsKey(systemId)){
            return null;
        }
        return serverSystemMsgMap.get(systemId).get(groupId);
    }
}
